// 长链接传输
const express = require('express');
const app = express();
// 指定目录
console.log(__dirname+'/index/black.html');
app.use('/black',express.static(__dirname+'/index/black.html'));
app.use('/white',express.static(__dirname+'/index/white.html'));
const title = 'socket服务器启动成功: http://192.168.10.168:3020';
const server = app.listen(3020,() => console.log(title));
const io = require('socket.io').listen(server);
io.on('connection',socket => {
    console.log(`客户端连接到了服务器可以开始发送数据了`);
    // 服务器发送的数据(客户端接收的数据)
    socket.emit('blackChat',{ status: 0,message: '小黑连接服务器成功!',data: "发送数据到：whiteChat" });
    socket.emit('whiteChat',{ status: 0,message: '小白连接服务器成功!',data: "发送数据到：blackChat" });
    // 客户端发送的数据A(服务器接收的数据)小黑
    socket.on('blackChat',data => {
        console.log('客户端A[发送]的内容',data.whiteChat,'whiteChat',data);
        io.sockets.emit('whiteChat', {
            whiteChat: `${ data['whiteChat'] }`,
            time: new Date(),
            username: `${ data['username'] }`,
			id: 'whiteChat'
        });
    });
    // 客户端发送的数据B(服务器接收的数据)小白
    socket.on('whiteChat',data => {
        console.log('客户端B[发送]的内容',data.blackChat,'blackChat',data);
        io.sockets.emit('blackChat', {
            blackChat: `${ data['blackChat'] }`,
            time: new Date(),
            username: `${ data['username'] }`,
			id: 'blackChat'
        });
    });
});
